package cn.psvmc.cxzapi.service.goods;

import cn.psvmc.cxzapi.config.mybatisplus.service.impl.AbstractService;
import cn.psvmc.cxzapi.exception.ServiceException;
import cn.psvmc.cxzapi.mapper.goods.*;
import cn.psvmc.cxzapi.model.common.GoodsEntity;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.goods.*;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.alipay.AlipayCreate;
import cn.psvmc.cxzapi.service.system.ServiceUtil;
import cn.psvmc.cxzapi.util.CodeUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 商品表 服务实现类
 *
 * @Author auto
 * @Date 2024-08-13
 */
@Service
public class TGoodsService extends AbstractService<TGoodsMapper, TGoods> {

    @Resource
    private TGoodsMapper tGoodsMapper;
    @Resource
    private TGoodsLogisticsMapper goodsLogisticsMapper;
    @Resource
    private TGoodsRentMapper goodsRentMapper;
    @Resource
    private TGoodsAttributeMapper goodsAttributeMapper;
    @Resource
    private TGoodsSpecsMapper goodsSpecsMapper;
    @Resource
    private TGoodsSpecsRentMapper goodsSpecsRentMapper;
    @Resource
    private TGoodsSpecsRentService goodsSpecsRentService;
    @Resource
    private TGoodsRentService goodsRentService;
    @Resource
    private TGoodsAttributeService goodsAttributeService;
    @Resource
    private TGoodsSpecsService goodsSpecsService;
    @Resource
    private TGoodsLogisticsMapper tGoodsLogisticsMapper;
    @Resource
    private TGoodsTagMapper goodsTagMapper;
    @Resource
    private ServiceUtil serviceUtil;
    @Resource
    private TGoodsExampleMapper tGoodsExampleMapper;

    /**
     * @Description 保存商品类目信息
     * @Date 2024/8/14 8:47
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @Transactional(rollbackFor = Exception.class)
    public Result step1(TGoods tGoods) {
        Integer goodsId = tGoods.getGoodsId();
        TGoods goods = tGoodsMapper.selectById(goodsId);
        Integer id = 0;
        if (id.equals(tGoods.getBrandId())) {
            tGoods.setBrandId(null);
        }
        if (id.equals(tGoods.getBmid())) {
            tGoods.setBmid(null);
        }
        if (ToolUtils.isTrueInteger(goodsId) && goods != null) {
            tGoods.setStep(null);
            update(tGoods);//修改商品信息
            Integer sortId = tGoods.getSortId();
            if (ToolUtils.isTrueInteger(sortId) && !goods.getSortId().equals(sortId)) {
                goodsTagMapper.deleteByGoodsId(goodsId);
                saveGoodsTag(tGoods);//保存商品标签
            }
            tGoods.setStep(goods.getStep());
        } else {
            tGoods.setStep(1);
            tGoods.setGoodsCode(CodeUtil.generateGoodsCode());
            tGoodsMapper.insert(tGoods);

            saveGoodsTag(tGoods);//保存商品标签
        }
        return ResultGenerator.genSuccessResult("保存成功！", tGoods);
    }

    //保存商品标签
    private void saveGoodsTag(TGoods tGoods) {
        TGoodsTag tag = new TGoodsTag();
        tag.setGoodsId(tGoods.getGoodsId());
        tag.setName(tGoods.getSortName());
        goodsTagMapper.insert(tag);
    }

    //修改商品信息
    private void update(TGoods tGoods) {
        tGoodsMapper.updateById(tGoods);
    }

    /**
     * @Description 保存商品信息
     * @Date 2024/8/14 9:05
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    public Result step2(TGoods tGoods) {
        Integer goodsId = tGoods.getGoodsId();
        TGoods originGoods = tGoodsMapper.selectById(goodsId);
        checkGoods(originGoods);//校验商品
        if (originGoods.getStep().equals(1)) {//没保存过商品信息
            tGoods.setStep(2);
        } else {//保存过商品信息
            tGoods.setStep(null);
        }
        if (ToolUtils.isEmpty(originGoods.getGoodsCode())) {
            tGoods.setGoodsCode(CodeUtil.generateGoodsCode());
        }
        update(tGoods);//修改商品信息
        //更新商品步数
        if (!ToolUtils.isTrueInteger(tGoods.getStep())) {
            tGoods.setStep(originGoods.getStep());
        }
        return ResultGenerator.genSuccessResult("保存成功！", tGoods);
    }

    /**
     * @Description 保存销售信息
     * @Date 2024/8/14 9:14
     * @Author YWX
     * @Param [tGoods]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @Transactional(rollbackFor = Exception.class)
    public Result step3(TGoods tGoods) {
        Integer goodsId = tGoods.getGoodsId();
        TGoods originGoods = tGoodsMapper.selectById(goodsId);
        checkGoods(originGoods);//校验商品
        int type;
        if (originGoods.getStep().compareTo(2) > 0) {//保存过销售信息
            tGoods.setStep(null);
            goodsRentMapper.deleteByGoodsId(goodsId);//删除商品销售规格信息
            goodsAttributeMapper.deleteByGoodsId(goodsId);//删除商品销售规格信息
            //goodsSpecsMapper.deleteByGoodsId(goodsId);//删除商品销售规格信息
            goodsSpecsRentMapper.deleteByGoodsId(goodsId);//删除商品销售规格租金信息
            type = 2;
        } else {//没保存过销售信息
            tGoods.setStep(3);
            type = 1;
        }
        saveGoodSale(tGoods, type);//保存销售信息

        update(tGoods);//修改商品信息
        //商品免审更新
        if (originGoods.getGoodsType().equals(1)) {
            tGoods.setGoodsCode(originGoods.getGoodsCode());
            goodsDirectModify(tGoods);
        }
        //更新商品步数
        if (!ToolUtils.isTrueInteger(tGoods.getStep())) {
            tGoods.setStep(originGoods.getStep());
        }
        tGoods.setRents(null);
        tGoods.setAttributes(null);
        tGoods.setSpecs(null);
        return ResultGenerator.genSuccessResult("保存成功！", tGoods);
    }

    //保存销售信息
    private void saveGoodSale(TGoods tGoods, int addOrEdit) {
        Integer goodsId = tGoods.getGoodsId();

        //计算租金范围、租期范围、押金范围和销售价格范围、总库存
        Double minRent = null, maxRent = null;
        Double minDeposit = null, maxDeposit = null;
        Double minPrice = null, maxPrice = null;
        int stock = 0;

        //商品租赁信息
        List<TGoodsRent> rents = tGoods.getRents();
        Map<Integer, Integer> maxDayMap = new HashMap<>();//租赁方案最高天数map
        for (TGoodsRent r : rents) {
            r.setGoodsId(goodsId);
            maxDayMap.put(r.getType(), r.getMaxDay());
        }
        goodsRentService.saveBatch(rents);

        //商品属性信息
        List<TGoodsAttribute> attributes = tGoods.getAttributes();
        attributes.forEach(a -> a.setGoodsId(goodsId));
        goodsAttributeService.saveBatch(attributes);

        //商品规格信息
        //Integer userId = serviceUtil.getUserId();
        Integer userId = 3;
        LocalDateTime now = LocalDateTime.now();
        List<TGoodsSpecs> specs = tGoods.getSpecs();
        int specsOrder = 1;
        int totalStock = 0;
        for (TGoodsSpecs s : specs) {
            if (s.getOutStock().equals(1)) {
                s.setStock(0);
            } else {
                totalStock += s.getStock();
            }
            s.setSpecsOrder(specsOrder);
            specsOrder++;
            s.setGoodsId(goodsId);
            stock += s.getStock();
            double deposit = s.getDeposit();
            if (minDeposit == null || deposit < minDeposit) {
                minDeposit = deposit;
            }
            if (maxDeposit == null || deposit > maxDeposit) {
                maxDeposit = deposit;
            }
            double buyPrice = s.getBuyPrice();
            if (minPrice == null || buyPrice < minPrice) {
                minPrice = buyPrice;
            }
            if (maxPrice == null || buyPrice > maxPrice) {
                maxPrice = buyPrice;
            }
            s.setCreateid(userId);
            s.setCreatetime(now);
        }
        tGoods.setStock(totalStock);
        if (addOrEdit == 1) {//添加
            goodsSpecsService.saveBatch(specs);
        } else {//更新
            List<TGoodsSpecs> specsList = goodsSpecsMapper.selectList(new QueryWrapper<TGoodsSpecs>().eq("goods_id", goodsId));
            for (TGoodsSpecs o : specsList) {
                Integer type = o.getType();
                String name = o.getName();
                Integer gsid = o.getGsid();
                boolean exist = false;
                for (TGoodsSpecs s : specs) {
                    if (s.getType().equals(type) && s.getName().equals(name)) {
                        exist = true;
                        break;
                    }
                }
                if (!exist) {//如果原有的规格不存在，则标记删除
                    goodsSpecsMapper.deleteById(gsid);
                }
            }
            List<TGoodsSpecs> addSpecs = new ArrayList<>();
            List<TGoodsSpecs> updateSpecs = new ArrayList<>();
            for (TGoodsSpecs spec : specs) {
                boolean exist = false;
                for (TGoodsSpecs o : specsList) {
                    if (o.getType().equals(spec.getType()) && o.getName().equals(spec.getName())) {
                        exist = true;
                        spec.setGsid(o.getGsid());
                        break;
                    }
                }
                if (exist) {
                    goodsSpecsMapper.updateById(spec);
                } else {
                    addSpecs.add(spec);
                }
            }
            if (ToolUtils.isListNotEmpty(addSpecs)) {
                goodsSpecsService.saveBatch(addSpecs);
            }
        }

        List<TGoodsSpecsRent> rentList = new ArrayList<>();
        for (TGoodsSpecs spec : specs) {
            Integer gsid = spec.getGsid();
            Integer outStock = spec.getOutStock();
            List<TGoodsSpecsRent> dayRents = spec.getDayRents();
            for (TGoodsSpecsRent specsRent : dayRents) {
                specsRent.setGoodsId(goodsId);
                specsRent.setGsid(gsid);
                Double rent1 = specsRent.getRent();
                if (maxRent == null || (outStock.equals(0) && ToolUtils.isTrueDoule(rent1))) {//不缺货,并且有租金
                    double rent = ToolUtils.divideAndCeil(rent1, specsRent.getDay());
                    if (minRent == null || rent < minRent) {
                        minRent = rent;
                    }
                    if (maxRent == null || rent > maxRent) {
                        maxRent = rent;
                    }
                }
            }
            rentList.addAll(dayRents);
        }
        goodsSpecsRentService.saveBatch(rentList);

        //设置租金范围、租期范围、押金范围和销售价格范围、总库存
        tGoods.setPriceRange(ToolUtils.formatDouble(minRent) + "-" + ToolUtils.formatDouble(maxRent));
        int minDay = rentList.stream().mapToInt(TGoodsSpecsRent::getDay).min().getAsInt();
        int maxDay = rentList.stream().mapToInt(TGoodsSpecsRent::getDay).max().getAsInt();
        tGoods.setDayRange(minDay + "-" + maxDay);
        tGoods.setDepositRange(ToolUtils.formatDouble(minDeposit) + "-" + ToolUtils.formatDouble(maxDeposit));
        tGoods.setSellPrice(ToolUtils.formatDouble(minPrice) + "-" + ToolUtils.formatDouble(maxPrice));
        tGoods.setStock(stock);
    }

    /**
     * @Description 保存物流信息
     * @Date 2024/8/14 9:36
     * @Author YWX
     * @Param [logistics]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    public Result step4(TGoodsLogistics logistics) {
        Integer goodsId = logistics.getGoodsId();
        TGoods originGoods = tGoodsMapper.selectById(goodsId);
        checkGoods(originGoods);//校验商品
        if (originGoods.getStep().equals(4)) {//保存过商品物流信息
            tGoodsLogisticsMapper.deleteByGoodsId(goodsId);
        }
        tGoodsLogisticsMapper.insert(logistics);
        TGoods tGoods = new TGoods();
        tGoods.setGoodsId(goodsId);
        tGoods.setStep(4);

        tGoods.setCheckStatus(0);
        tGoods.setGoodsStatus(0);
        update(tGoods);//修改商品信息
        return ResultGenerator.genSuccessResult("保存成功！", tGoods);
    }

    //校验商品
    private void checkGoods(TGoods originTGoods) {
        if (originTGoods == null) {
            throw new ServiceException("未查询到对应的商品表信息！");
        }
    }

    //校验商品和版本号
    private void checkGoodsAndVersion(TGoods tGoods) {
        // 通过id查询数据处理 详情
        TGoods originTGoods = tGoodsMapper.selectById(tGoods.getGoodsId());
        if (originTGoods == null) {
            throw new ServiceException("未查询到对应的商品表信息！");
        }

        // 比较数据版本号
        if (!originTGoods.getRversion().equals(tGoods.getRversion())) {
            throw new ServiceException("数据已更新，请刷新后重试！");
        }
    }

    /**
     * @Date auto 2024-08-13 查询商品表详情
     * @Param id 唯一id
     * @Return cn.psvmc.cxzapi.model.goods.TGoods
     */
    public Map<String, Object> detail(Integer id) {
        return tGoodsMapper.findById(id);
    }

    /**
     * @Description 销售信息
     * @Date 2024/8/14 9:49
     * @Author YWX
     * @Param [id]
     * @Return java.util.Map<java.lang.String, java.lang.Object>
     **/
    public Map<String, Object> detail3(Integer id) {
        Map<String, Object> map = new HashMap<>();
        TGoods goods = tGoodsMapper.selectById(id);
        checkGoods(goods);
        map.put("goodsId",goods.getGoodsId());
        map.put("tipComm",goods.getTipComm());
        map.put("rents", goodsRentMapper.listByGoodsId(id));//租赁方案
        map.put("attributes", goodsAttributeMapper.listByGoodsId(id));//商品属性
        map.put("specs", goodsSpecsMapper.listByGoodsId(id));//销售规格
        return map;
    }

    /**
     * @Description 物流信息
     * @Date 2024/8/14 9:58
     * @Author YWX
     * @Param [id]
     * @Return java.util.Map<java.lang.String, java.lang.Object>
     **/
    public Map<String, Object> detail4(Integer id) {
        return goodsLogisticsMapper.findByGoodsId(id);
    }

    /**
     * @Description 商品二维码
     * @Date 2024/8/14 17:02
     * @Author YWX
     * @Param [id]
     * @Return java.util.Map<java.lang.String, java.lang.Object>
     **/
    public Map<String, Object> detailCode(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("goods", tGoodsMapper.detailCode(id));//商品信息
        map.put("specs", goodsSpecsMapper.getMinRentByGoodsId(id));//最低租金销售规格
        return map;
    }

    /**
     * @Date auto 2024-08-13 查询商品表列表
     * @Param iPage 分页
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<cn.psvmc.cxzapi.model.goods.TGoods>
     */
    public IPage<Map> list(Page<TGoods> iPage, SearchEntity entity) {
        serviceUtil.setStoreId(entity);
        if (!ToolUtils.isTrueInteger(entity.getStoreid())) {
            throw new ServiceException("商家id不能为空！");
        }
        IPage<Map> page = tGoodsMapper.listAll(iPage, entity);
        String storename = serviceUtil.getStoreName(entity.getStoreid());
        page.getRecords().forEach(g -> g.put("storename", storename));
        return page;
    }

    public IPage<Map> listPt(Page<TGoods> iPage, SearchEntity entity) {
        return tGoodsMapper.listPt(iPage, entity);
    }

    /**
     * @Description 修改商品状态
     * @Date 2024/8/13 22:26
     * @Author YWX
     * @Param [goodsId, goodsStatus]
     * @Return void
     **/
    public void updateStatus(Integer goodsId, int goodsStatus) {
        TGoods goods = new TGoods();
        goods.setGoodsId(goodsId);
        goods.setGoodsStatus(goodsStatus);
        tGoodsMapper.updateById(goods);
    }

    /**
     * @Description 审核
     * @Date 2024/8/13 22:33
     * @Author YWX
     * @Param [tGoods]
     * @Return void
     **/
    public void check(TGoods tGoods) {
        checkGoodsAndVersion(tGoods);//校验商品和版本号
        if (tGoods.getCheckStatus().equals(1)) {//审核通过，上架商品
            tGoods.setGoodsStatus(1);
        } else {//审核拒绝，系统下架商品
            tGoods.setGoodsStatus(4);
        }
        tGoods.setCheckId(serviceUtil.getUserId());
        tGoods.setCheckTime(LocalDateTime.now());
        tGoodsMapper.updateById(tGoods);
    }

    /**
     * @Description 编辑总库存
     * @Date 2024/8/14 15:06
     * @Author YWX
     * @Param [tGoods]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void editStock(Integer goodsId, List<TGoodsSpecs> specs) {
        TGoods originGoods = tGoodsMapper.selectById(goodsId);
        checkGoods(originGoods);//校验商品
        int stock = 0;
        for (TGoodsSpecs spec : specs) {
            if (!ToolUtils.isTrueInteger(spec.getGsid())) {
                throw new ServiceException("请选择规格！");
            }
            if (spec.getStock() == null) {
                throw new ServiceException("请输入库存！");
            }
            stock += spec.getStock();
            goodsSpecsMapper.updateById(spec);
        }
        tGoodsMapper.editStock(goodsId, stock);
        if (originGoods.getGoodsType().equals(1)) {
            originGoods.setStock(stock);
            goodsDirectModify(originGoods);
        }
    }

    /**
     * @Description 获取所有商品标签
     * @Date 2024/8/17 15:45
     * @Author YWX
     * @Param []
     * @Return java.util.List<java.lang.String>
     **/
    public List<String> listTag() {
        return goodsTagMapper.listTag();
    }

    /**
     * @Description 添加商品标签
     * @Date 2024/8/17 15:51
     * @Author YWX
     * @Param [vo]
     * @Return void
     **/
    public void addTag(TGoodsTagVo vo) {
        Integer goodsId = vo.getGoodsId();
        List<TGoodsTag> tags = new ArrayList<>();
        TGoodsTag gt;
        for (String tag : vo.getTags()) {
            gt = new TGoodsTag();
            gt.setGoodsId(goodsId);
            gt.setName(tag);
            tags.add(gt);
        }
        goodsTagMapper.insertBatchSomeColumn(tags);
    }

    /**
     * @Description 商品选择列表
     * @Date 2024/8/17 22:21
     * @Author YWX
     * @Param [queryPage, entity]
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map>
     **/
    public IPage<Map> listForSel(Page<TGoods> queryPage, SearchEntity entity) {
        serviceUtil.setStoreId(entity);
        return tGoodsMapper.listForSel(queryPage, entity);
    }

    /**
     * @Description 使用的分类列表
     * @Date 2024/8/24 15:11
     * @Author YWX
     * @Param []
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map> listUsedType() {
        return tGoodsMapper.listUsedType(serviceUtil.getStoreId());
    }

    /**
     * @Description 提交审核
     * @Date 2024/8/26 17:34
     * @Author YWX
     * @Param [goodsId]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    public Result commit(Integer goodsId) {
        TGoods goods = tGoodsMapper.selectById(goodsId);
        checkGoods(goods);
        if (!goods.getGoodsStatus().equals(0)) {
            throw new ServiceException("无需提交审核！");
        }
        TGoods tGoods = new TGoods();
        tGoods.setGoodsId(goodsId);
        tGoods.setGoodsStatus(3);
        tGoods.setCheckStatus(0);
        tGoodsMapper.updateById(tGoods);
        return ResultGenerator.genSuccessResult("提交成功！");
    }

    /**
     * @Description 删除
     * @Date 2024/8/30 22:27
     * @Author YWX
     * @Param [id, rversion]
     * @Return int
     **/
    public int delete(Integer id, Integer rversion) {
        TGoods goods = new TGoods();
        goods.setGoodsId(id);
        goods.setRversion(rversion);
        checkGoodsAndVersion(goods);
        return tGoodsMapper.deleteById(goods);
    }

    /**
     * @Description 根据商品id获取库存
     * @Date 2024/9/19 21:29
     * @Author YWX
     * @Param [goodsId]
     * @Return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     **/
    public List<Map<String, Object>> listStock(Integer goodsId) {
        return goodsSpecsMapper.listStock(goodsId);
    }

    /**
     * @Description 复制商品
     * @Date 2024/9/20 13:04
     * @Author YWX
     * @Param [goodsId]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void copyGoods(Integer goodsId) {
        TGoods goods = tGoodsMapper.selectById(goodsId);
        checkGoods(goods);
        goods.setGoodsId(null);
        goods.setGoodsCode(CodeUtil.generateGoodsCode());
        goods.setGoodsStatus(0);
        goods.setCheckStatus(0);
        goods.setCheckComm(null);
        goods.setCheckId(null);
        goods.setCheckTime(null);
        goods.setDeleted(0);
        goods.setRversion(null);
        tGoodsMapper.insert(goods);
        Integer id = goods.getGoodsId();
        Integer createid = goods.getCreateid();
        LocalDateTime now = LocalDateTime.now();
        Integer step = goods.getStep();
        goodsTagMapper.copyGoodsTag(goodsId, id);//复制商品标签信息
        if (step.equals(4)) {
            goodsLogisticsMapper.copyGoodsLogistics(goodsId, id, createid, now);//复制商品物流信息
        }
        if (step >= 3) {
            goodsAttributeMapper.copyGoodsAttribute(goodsId, id, createid, now);//复制商品属性信息
            goodsRentMapper.copyGoodsRent(goodsId, id, createid, now);//复制商品租赁方案信息
            List<Map> specsList = goodsSpecsMapper.listByGoodsId(goodsId);
            for (Map map : specsList) {
                TGoodsSpecs spec = new TGoodsSpecs();
                spec.setGoodsId(id);
                spec.setType(ToolUtils.objToInteger(map.get("type")));
                spec.setName(ToolUtils.objToString(map.get("name")));
                spec.setOutStock(ToolUtils.objToInteger(map.get("outStock")));
                spec.setRelet(ToolUtils.objToInteger(map.get("relet")));
                spec.setDeposit(ToolUtils.objToDouble(map.get("deposit")));
                spec.setBuyPrice(ToolUtils.objToDouble(map.get("buyPrice")));
                spec.setStock(ToolUtils.objToInteger(map.get("stock")));
                spec.setSpecsOrder(ToolUtils.objToInteger(map.get("specsOrder")));
                goodsSpecsMapper.insert(spec);
                List<Map> dayRents = (List<Map>) map.get("dayRents");
                if (ToolUtils.isListEmpty(dayRents)) {
                    continue;
                }
                Integer gsid = spec.getGsid();
                List<TGoodsSpecsRent> rentList = new ArrayList<>();
                for (Map dayRent : dayRents) {
                    TGoodsSpecsRent rent = new TGoodsSpecsRent();
                    rent.setGoodsId(id);
                    rent.setGsid(gsid);
                    rent.setDay(ToolUtils.objToInteger(dayRent.get("day")));
                    rent.setRent(ToolUtils.objToDouble(dayRent.get("rent")));
                    rent.setEffective(ToolUtils.objToInteger(dayRent.get("effective")));
                    rentList.add(rent);
                }
                goodsSpecsRentService.saveBatch(rentList);
            }
        }
    }

    /**
     * @Description 根据商品id获取商品列表
     * @Date 2024/9/28 20:43
     * @Author YWX
     * @Param [entity]
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map> listGoodsByIds(GoodsEntity entity) {
        return tGoodsExampleMapper.listGoodsByIds(entity);
    }

    /**
     * @Description 撤销审核
     * @Date 2024/10/2 20:46
     * @Author YWX
     * @Param [goodsId]
     * @Return void
     **/
    public void cancelCheck(Integer goodsId) {
        Integer userId = serviceUtil.getUserId();
        tGoodsMapper.cancelCheck(goodsId, userId, LocalDateTime.now());
    }

    /**
     * @Description 更新是否是白名单商品
     * @Date 2024/10/10 21:54
     * @Author YWX
     * @Param [tGoods]
     * @Return void
     **/
    public void editIsWhite(TGoods tGoods) {
        checkGoodsAndVersion(tGoods);
        tGoodsMapper.updateById(tGoods);
    }

    /**
     * @Description 平台下架商品
     * @Date 2024/10/28 22:53
     * @Author YWX
     * @Param [tGoods]
     * @Return void
     **/
    public void plateformDown(TGoods tGoods) {
        checkGoodsAndVersion(tGoods);//校验商品和版本号
        tGoods.setGoodsStatus(4);
        tGoods.setCheckStatus(2);
        tGoods.setCheckId(serviceUtil.getUserId());
        tGoods.setCheckTime(LocalDateTime.now());
        tGoodsMapper.updateById(tGoods);
    }

    /**
     * @Description 更新免审商品信息
     * @Date 2024/12/14 23:00
     * @Author YWX
     * @Param []
     * @Return void
     **/
    public void goodsDirectModify() {
        List<TGoods> list = tGoodsMapper.listTaskGoods();
        if (ToolUtils.isListEmpty(list)) {
            return;
        }
        for (TGoods goods : list) {
            goodsDirectModify(goods);
        }
    }

    @Async("asyncPoolTaskExecutor")
    public void goodsDirectModify(TGoods goods) {
        boolean result = AlipayCreate.goodsDirectModify(goods);
        if (result) {
            tGoodsMapper.updateNeedSyncByGoodsCode(goods.getGoodsCode(), 0);
        }
    }

    /**
     * @Description 根据商品id更新商品类型
     * @Date 2024/12/14 23:11
     * @Author YWX
     * @Param [goodsId, goodsType]
     * @Return void
     **/
    public void updateGoodsType(Integer goodsId, Integer goodsType) {
        tGoodsMapper.updateGoodsTypeById(goodsId, goodsType);
    }
}
